package com.ellricky.newtv

import android.content.pm.ActivityInfo
import android.graphics.Color
import android.view.View
import android.widget.Toast
import androidx.recyclerview.widget.GridLayoutManager
import com.andr.common.ui.activity.BaseActivity
import com.andr.common.ui.mvp.BasePresenter
import com.andr.common.util.SpfUtil
import com.andr.common.util.ToastUtil
import com.ellricky.baselib.adapter.GridItemDecoration
import com.ellricky.baselib.cache.LocalCacheUtil
import com.ellricky.newtv.adapter.ClassAdapter
import com.ellricky.baselib.entity.newtv.ClassInfo
import com.ellricky.baselib.http.HttpUtils
import com.ellricky.baselib.util.EAppUtil
import com.ellricky.baselib.util.NewEListCallback
import com.ellricky.newtv.utils.CacheToken
import com.google.gson.Gson
import kotlinx.android.synthetic.main.activity_new_tv_bind_class.*

class NewTvBindClassActivity : BaseActivity<Void, BasePresenter<Void>>() {

    private val classAdapter by lazy {
        ClassAdapter()
    }

    override fun getContentLayout(): Int {
        return R.layout.activity_new_tv_bind_class
    }

    override fun setOrientation() {
        this.requestedOrientation =
            ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
    }

    override fun initUIView() {
        CacheToken.verifyToken()
        tv_back.setOnClickListener(this)
        tv_bind.setOnClickListener(this)
        initRv()
        loadData()
    }

    private fun loadData() {
        val url = EAppUtil.getTvApiHost()
        val paramMap = mutableMapOf(
            "action" to "classList",
            "eid" to LocalCacheUtil.getTvUid()
        )
        HttpUtils.getInstance()
            .postFrom(url, paramMap, object : NewEListCallback<ClassInfo>(this, false) {
                override fun onSuccess(data: MutableList<ClassInfo>, msg: String?) {
                    classAdapter.setList(data)
                }
            }, this)
    }

    override fun onClickView(v: View) {
        super.onClickView(v)
        when (v.id) {
            R.id.tv_back -> {
                finish()
            }
            R.id.tv_bind -> {
                val classInfo = getBindClass()
                if (classInfo == null) {
                    Toast.makeText(this, "请选择一个班级进行关联", Toast.LENGTH_SHORT).show()
                    return
                } else {
                    LocalCacheUtil.saveTvBindClass(classInfo)
                    ToastUtil.show(this, "关联成功")
                    finish()
                }
            }
        }
    }

    private fun getBindClass(): ClassInfo? {
        classAdapter.data.forEach {
            if (it.isSelect) {
                return it
            }
        }
        return null
    }

    private fun initRv() {
        rv_class.layoutManager = GridLayoutManager(this, 6)
        rv_class.adapter = classAdapter
        rv_class.addItemDecoration(GridItemDecoration(20, 30, Color.TRANSPARENT))
        classAdapter.addChildClickViewIds(R.id.cl_class)
        classAdapter.setOnItemChildClickListener { _,
                                                   view,
                                                   position ->
            when (view.id) {
                R.id.cl_class -> {
                    val data = classAdapter.getItem(position)
                    classAdapter.data.forEach {
                        it.isSelect = false
                    }
                    data.isSelect = !data.isSelect
                    classAdapter.notifyDataSetChanged()
                }
            }
        }
    }
}